package com.example.myys.network.mhPlugins

import com.alibaba.fastjson2.JSON
import com.example.myys.Constants
import com.example.myys.data.entity.ChapterBean
import com.example.myys.data.entity.DataBean
import com.example.myys.data.repository.SettingLinkRepository
import com.example.myys.network.HttpClient
import com.example.myys.network.parse.HtmlParse
import com.example.myys.util.FileUtil
import org.jsoup.Jsoup
import org.jsoup.nodes.Element

class MhHtmlParse : HtmlParse() {
    private val isChecked = SettingLinkRepository.getSettingByTypeIsChecked(Constants.MH)
    private val dataJson =
        if (isChecked != null) JSON.parseObject(isChecked.value) else JSON.parseArray(
            FileUtil.fileToJsonStr("mhjk.json")
        ).getJSONObject(0)

    override suspend fun getDataRecommend(createDate: String): List<DataBean> {
        var dataHtmlList = ArrayList<DataBean>()
        val url = dataJson.getString("url");
        val tj = dataJson.getJSONObject("tj")
        var tjUrl = tj.getString("url")
        if (!tjUrl.startsWith("http")) {
            tjUrl = url + tjUrl
        }
        var html = HttpClient.get(tjUrl, mapOf(), dataJson.getString("ecode"))
        if (html == null || html.isEmpty()) {
            return dataHtmlList
        }
        val document = Jsoup.parse(html)
        val tjList = tj.getJSONObject("list")
        val tjXpath = tjList.getString("xpath")
        val elementList = document.select(tjXpath)
        val tjListItem = tjList.getJSONObject("item")
        val tjListItemXpath = tjListItem.getString("xpath")
        val elements = elementList.select(tjListItemXpath)
        for (element in elements) {
            var dataHtmlBean = DataBean()
            tjListItem.getList("image", String::class.java)?.let {
                dataHtmlBean.cover = element.select(it.get(0)).attr(it.get(1))
            }
            tjListItem.getString("name")?.let {
                dataHtmlBean.name = element.select(it).text()
            }
            tjListItem.getList("url", String::class.java)?.let {
                dataHtmlBean.url = element.select(it.get(0)).attr(it.get(1))
            }
            tjListItem.getString("zz")?.let {
                dataHtmlBean.author = element.select(it).text()
            }
            tjListItem.getString("jj")?.let {
                dataHtmlBean.desc = element.select(it).text()
            }
            tjListItem.getString("zt")?.let {
                dataHtmlBean.status = element.select(it).text()
            }

            dataHtmlList.add(dataHtmlBean)
        }
        return dataHtmlList
    }

    override suspend fun getDataInfo(dataUrl: String): DataBean? {
        var dataHtml = DataBean()
        var chapterList = ArrayList<ChapterBean>()
        var url = dataJson.getString("url");
        val xqJson = dataJson.getJSONObject("xq")
        if (!dataUrl.startsWith("http")) {
            url += dataUrl
        } else {
            url = dataUrl
        }
        val html = HttpClient.get(url, mapOf(), dataJson.getString("ecode"))
        if (html == null || html.isEmpty()) {
            return dataHtml
        }
        val document = Jsoup.parse(html)
        xqJson.getList("image", String::class.java)?.let {
            dataHtml.cover = document.select(it.get(0)).attr(it.get(1))
        }
        xqJson.getString("name")?.let {
            dataHtml.name = document.select(it).text()
        }
        xqJson.getList("url", String::class.java)?.let {
            dataHtml.url = document.select(it.get(0)).attr(it.get(1))
        }
        xqJson.getString("zz")?.let {
            dataHtml.author = document.select(it).text()
        }
        xqJson.getString("jj")?.let {
            dataHtml.desc = document.select(it).text()
        }
        xqJson.getString("zt")?.let {
            dataHtml.status = document.select(it).text()
        }
        val xqZjJson = xqJson.getJSONObject("zj")
        val zjXpath = xqZjJson.getString("xpath")
        val dataHtmllistmain = document.select(zjXpath)
        val xqZjItemJson = xqZjJson.getJSONObject("item")
        val xqZjItemXpath = xqZjItemJson.getString("xpath")
        val listmaindds = dataHtmllistmain.select(xqZjItemXpath)
        for ((index, listmaindd: Element) in listmaindds.withIndex()) {
            var chapter = ChapterBean()
            xqZjItemJson.getString("name")?.let {
                chapter.name = listmaindd.select(it).text()
            }
            xqZjItemJson.getString("url")?.let {
                if (it == "-") {
                    chapter.url = listmaindd.attr("href")
                } else {
                    chapter.url = listmaindd.select(it).attr("href")
                }
            }
            chapter.index = index
            chapterList.add(chapter)
        }
        return dataHtml
    }

    override suspend fun getChapterList(chaptersUrl: String, dataBeanId: Int): List<ChapterBean> {
        var chapterList = ArrayList<ChapterBean>()
        var url = dataJson.getString("url");
        val xqJson = dataJson.getJSONObject("xq")
        if (!chaptersUrl.startsWith("http")) {
            url += chaptersUrl
        } else {
            url = chaptersUrl
        }
        val html = HttpClient.get(url, mapOf(), dataJson.getString("ecode"))
        if (html == null || html.isEmpty()) {
            return chapterList
        }
        val document = Jsoup.parse(html)
        val xqZjJson = xqJson.getJSONObject("zj")
        val zjXpath = xqZjJson.getString("xpath")
        val dataHtmllistmain = document.select(zjXpath)
        val xqZjItemJson = xqZjJson.getJSONObject("item")
        val xqZjItemXpath = xqZjItemJson.getString("xpath")
        val listmaindds = dataHtmllistmain.select(xqZjItemXpath)
        for ((index, listmaindd: Element) in listmaindds.withIndex()) {
            var chapter = ChapterBean()
            chapter.dataBeanId = dataBeanId
            xqZjItemJson.getString("name")?.let {
                chapter.name = listmaindd.select(it).text()
            }
            xqZjItemJson.getString("url")?.let {
                if (it == "-") {
                    chapter.url = listmaindd.attr("href")
                } else {
                    chapter.url = listmaindd.select(it).attr("href")
                }
            }
            chapter.index = index
            chapterList.add(chapter)
        }
        return chapterList
    }

    override suspend fun getChapterContent(chapterUrl: String): String {
        var content = ""
        var url = dataJson.getString("url");
        val nrJson = dataJson.getJSONObject("nr")
        if (!chapterUrl.startsWith("http")) {
            url += chapterUrl
        } else {
            url = chapterUrl
        }
        val html = HttpClient.get(url, mapOf(), dataJson.getString("ecode"))
        //val html=HttpClient.getWebviewUrl(url, "")
        if (html == null) {
            return ""
        }
        val document = Jsoup.parse(html)
        val jsonList = nrJson.getString("nrlist")
        val listHtmlJson = document.select(jsonList)
        val jsonListItem = nrJson.getString("zjnr")
        val listHtmlItemJson = listHtmlJson.select(jsonListItem)
        for ((index, listitem: Element) in listHtmlItemJson.withIndex()) {
            val jsonItemList = nrJson.getList("zjm", String::class.java)
            if (jsonItemList.get(0) == "-") {
                val si = listitem.attr(jsonItemList.get(1))
                content += si + ","
            } else {
                val si = listitem.select(jsonItemList.get(0)).attr(jsonItemList.get(1))
                content += si + ","
            }
        }
        //content = content.substring(0, content.length - 1)
        println(html)
        println(content)
        return content
    }

    override suspend fun getSearchByKeyword(keyword: String): List<DataBean> {
        var dataHtmlList = ArrayList<DataBean>()
        val url = dataJson.getString("url");
        val ss = dataJson.getJSONObject("ss")
        var ssUrl = ss.getString("url")
        if (!ssUrl.startsWith("http")) {
            ssUrl = url + ssUrl
        }
        ssUrl = ssUrl.replace(":keyWord", keyword)
        var html = HttpClient.getWebviewUrl(ssUrl, "")
        if (html == null || html.isEmpty()) {
            return dataHtmlList
        }
        val document = Jsoup.parse(html)
        val ssList = ss.getJSONObject("list")
        val ssXpath = ssList.getString("xpath")
        val elementList = document.select(ssXpath)
        val ssListItem = ssList.getJSONObject("item")
        val ssListItemXpath = ssListItem.getString("xpath")
        val elements = elementList.select(ssListItemXpath)
        for (element in elements) {
            var dataHtmlBean = DataBean()
            ssListItem.getList("image", String::class.java)?.let {
                dataHtmlBean.cover = element.select(it.get(0)).attr(it.get(1))
            }
            ssListItem.getString("name")?.let {
                dataHtmlBean.name = element.select(it).text()
            }
            ssListItem.getList("url", String::class.java)?.let {
                dataHtmlBean.url = element.select(it.get(0)).attr(it.get(1))
            }
            ssListItem.getString("zz")?.let {
                dataHtmlBean.author = element.select(it).text()
            }
            ssListItem.getString("jj")?.let {
                dataHtmlBean.desc = element.select(it).text()
            }
            ssListItem.getString("zt")?.let {
                dataHtmlBean.status = element.select(it).text()
            }
            dataHtmlList.add(dataHtmlBean)
        }
        return dataHtmlList
    }
}